home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / blandmdi.zip / BLANDMDI.C < prev    next >
C/C++ Source or Header  |  1991-05-03  |  8KB  |  287 lines

  1. /*
  2.  *
  3.  *  PROGRAM  : BlandMDI.c
  4.  *
  5.  *  PURPOSE  : Show example of a minimal MDI application
  6.  *
  7.  *  FUNCTIONS:
  8.  *
  9.  *      WinMain() - Calls the initialization function
  10.  *                  and processes message loop
  11.  *
  12.  *      BlandFrameWndProc() - Window function for the "frame"
  13.  *                  window, which controls the menu
  14.  *                  and contains the MDI client window
  15.  *
  16.  *      BlandMDIChildWndProc()- Window function for the individual
  17.  *                  child windows
  18.  *
  19.  *      CloseAllChildren - Destroys all MDI child windows.
  20.  *
  21.  *      CommandHandler() - Processes the "frame" window's
  22.  *                  WM_COMMAND messages.
  23.  *
  24.  * Copyright 1991 Microsoft Corporation. All rights reserved.
  25.  */
  26.  
  27.  
  28. /*--------------------  #includes  -------------------------------*/
  29.  
  30. #include "blandmdi.h"
  31.  
  32.  
  33. /*--------------------  global variables  -------------------------*/
  34.  
  35. // global variables used in this module or among more than one module
  36.  
  37. HANDLE hInst;                   // Program instance handle
  38. HWND hwndFrame     = NULL;      // Handle to main window
  39. HWND hwndMDIClient = NULL;      // Handle to MDI client
  40. LONG styleDefault  = 0;         // Default style bits for child windows
  41.  
  42.  
  43. /*---------------------  function prototypes  -----------------------*/
  44.  
  45. // Forward declarations of helper functions in this module
  46.  
  47. VOID NEAR PASCAL CommandHandler  (HWND,WORD);
  48. VOID NEAR PASCAL CloseAllChildren(VOID);
  49.  
  50.  
  51. /*---------------  BlandFrameWndProc()  -------------------------------*/
  52. /*
  53.  *
  54.  *  FUNCTION   : BlandFrameWndProc (hwnd, msg, wParam, lParam )
  55.  *
  56.  *  PURPOSE    : The window function for the "frame" window, which controls
  57.  *               the menu and encompasses all the MDI child windows. Does
  58.  *               the major part of the message processing. Specifically, in
  59.  *               response to:
  60.  *
  61.  *               WM_CREATE : Creates and displays the "frame".
  62.  *
  63.  *               WM_COMMAND: Passes control to a command-handling function.
  64.  *
  65.  *               WM_CLOSE  : Quits the app. 
  66.  *
  67.  *               WM_DESTROY: Destroys frame window and quits app.
  68.  *
  69.  *  NOTE: If cases are added for WM_MENUCHAR, WM_NEXTMENU, WM_SETFOCUS,
  70.  *        and WM_SIZE, note that these messages should be passed on
  71.  *        to DefFrameProc even if we handle them.  See the SDK Reference
  72.  *        entry for DefFrameProc
  73.  */
  74.  
  75.  
  76. LONG FAR PASCAL BlandFrameWndProc ( hwnd, msg, wParam, lParam )
  77.  
  78. register HWND hwnd;
  79. WORD          msg;
  80. register WORD wParam;
  81. LONG          lParam;
  82.  
  83. {
  84.     switch (msg)
  85.         {
  86.         case WM_CREATE:
  87.             {
  88.             CLIENTCREATESTRUCT ccs;
  89.  
  90.             // Find window menu where children will be listed 
  91.             ccs.hWindowMenu  = GetSubMenu (GetMenu(hwnd),WINDOWMENU);
  92.             ccs.idFirstChild = IDM_WINDOWCHILD;
  93.  
  94.             // Create the MDI client 
  95.             hwndMDIClient = CreateWindow ("mdiclient",
  96.                 NULL,
  97.                 WS_CHILD | WS_CLIPCHILDREN,
  98.                 0,
  99.                 0,
  100.                 0,
  101.                 0,
  102.                 hwnd,
  103.                 0,
  104.                 hInst,
  105.                 (LPSTR)&ccs);
  106.             ShowWindow (hwndMDIClient,SW_SHOW);
  107.             break;
  108.             }
  109.  
  110.         case WM_COMMAND:
  111.             // Direct all menu selection or accelerator commands to 
  112.             // the CommandHandler function
  113.             CommandHandler (hwnd,wParam);
  114.             break;
  115.  
  116.         case WM_CLOSE:
  117.             DestroyWindow (hwnd);
  118.             break;
  119.  
  120.         case WM_DESTROY:
  121.             PostQuitMessage (0);
  122.             break;
  123.  
  124.         default:
  125.             // use DefFrameProc() instead of DefWindowProc(), since there
  126.             // are things that have to be handled differently because of MDI
  127.             
  128.             return DefFrameProc (hwnd,hwndMDIClient,msg,wParam,lParam);
  129.         }
  130.     return 0;
  131. }
  132.  
  133.  
  134. /*--------------------BlandMDIChildWndProc  ----------------------*/
  135. /*
  136.  *
  137.  *  FUNCTION: BlandMDIChildWndProc ( hwnd, msg, wParam, lParam )  
  138.  *
  139.  *  NOTE: If cases are added for WM_CHILDACTIVATE, WM_GETMINMAXINFO,
  140.  *        WM_MENUCHAR, WM_MOVE, WM_NEXTMENU, WM_SETFOCUS, WM_SIZE,
  141.  *        or WM_SYSCOMMAND, these messages should be passed on
  142.  *        to DefMDIChildProc even if we handle them. See the SDK
  143.  *        Reference entry for DefMDIChildProc
  144.  *
  145.  */
  146.  
  147.  
  148. LONG FAR PASCAL BlandMDIChildWndProc ( hwnd, msg, wParam, lParam )
  149.  
  150. register HWND hwnd;
  151. WORD          msg;
  152. register WORD wParam;
  153. LONG          lParam;
  154.  
  155. {
  156.     // Since this is a generic MDI app, the children don't handle
  157.     // and messages in non-default ways
  158.     return DefMDIChildProc (hwnd, msg, wParam, lParam);
  159. }
  160.  
  161.  
  162.  
  163. /*-----------------  CommandHandler  -------------------------------*/
  164. /*
  165.  *
  166.  *  FUNCTION   : CommandHandler ()
  167.  *                    
  168.  *  PURPOSE    : Processes all "frame" WM_COMMAND messages.       
  169.  *                     
  170.  */
  171.  
  172. VOID NEAR PASCAL CommandHandler ( hwnd, wParam )
  173. register HWND hwnd;
  174. register WORD wParam;
  175.  
  176. {
  177.     switch (wParam)
  178.         {
  179.         case IDM_FILENEW:
  180.             // Make an empty MDI child window 
  181.             MakeNewChild (NULL);
  182.             break;
  183.  
  184.         case IDM_FILEEXIT:
  185.             // Close application 
  186.             SendMessage (hwnd, WM_CLOSE, 0, 0L);
  187.             break;
  188.  
  189.         case IDM_WINDOWTILE:
  190.             // Tile MDI windows 
  191.             SendMessage (hwndMDIClient, WM_MDITILE, 0, 0L);
  192.             break;
  193.  
  194.         case IDM_WINDOWCASCADE:
  195.             // Cascade MDI windows 
  196.             SendMessage (hwndMDIClient, WM_MDICASCADE, 0, 0L);
  197.             break;
  198.  
  199.         case IDM_WINDOWICONS:
  200.             // Auto - arrange MDI icons 
  201.             SendMessage (hwndMDIClient, WM_MDIICONARRANGE, 0, 0L);
  202.             break;
  203.  
  204.         case IDM_WINDOWCLOSEALL:
  205.             CloseAllChildren();
  206.             break;
  207.  
  208.         default:
  209.             // This is essential, since there are frame WM_COMMANDS generated
  210.             // by the MDI system for activating child windows via the
  211.             // window menu.
  212.             DefFrameProc(hwnd, hwndMDIClient, WM_COMMAND, wParam, 0L);
  213.         }
  214. }
  215.  
  216. /*-------------------  CloseAllChildren  -----------------------------*/
  217. /*
  218.  *
  219.  *  FUNCTION   : CloseAllChildren ()       
  220.  *                 
  221.  *  PURPOSE    : Destroys all MDI child windows.
  222.  *
  223.  */
  224.  
  225.  
  226. VOID NEAR PASCAL CloseAllChildren (VOID)
  227. {
  228.     register HWND hwndT;
  229.  
  230.     // As long as the MDI client has a child, destroy it 
  231.     while ( hwndT = GetWindow (hwndMDIClient, GW_CHILD))
  232.         {
  233.         // Skip the icon title windows 
  234.         while (hwndT && GetWindow (hwndT, GW_OWNER))
  235.             hwndT = GetWindow (hwndT, GW_HWNDNEXT);
  236.         if (hwndT)
  237.             SendMessage (hwndMDIClient, WM_MDIDESTROY, (WORD)hwndT, 0L);
  238.         else
  239.             break;
  240.         }
  241. }
  242.  
  243.  
  244.  
  245. /*-----------------------  WinMain  ----------------------------------*/
  246. /*
  247.  *
  248.  *  FUNCTION   : WinMain(HANDLE, HANDLE, LPSTR, int) 
  249.  *                     
  250.  *  PURPOSE    : Creates the "frame" window, does some initialization and  
  251.  *     enters the message loop.         
  252.  *              
  253.  */
  254.  
  255.  
  256. int NEAR PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  257.  
  258. HANDLE hInstance;
  259. HANDLE hPrevInstance;
  260. LPSTR  lpszCmdLine;
  261. int    nCmdShow;
  262. {
  263.     MSG msg;
  264.  
  265.     hInst = hInstance;
  266.  
  267.     // If this is the first instance of the app. register window classes 
  268.     if (!hPrevInstance)
  269.        if (!InitializeApplication ())
  270.          return 0;
  271.  
  272.     // Create the frame and do other initialization
  273.     if (!InitializeInstance (lpszCmdLine, nCmdShow))
  274.        return 0;
  275.  
  276.     // Enter main message loop 
  277.     while (GetMessage (&msg, NULL, 0, 0))
  278.         // If a keyboard message is for the MDI, let the MDI client
  279.         // take care of it.  Otherwise, just handle the message as usual
  280.         if ( !TranslateMDISysAccel (hwndMDIClient, &msg))
  281.             {
  282.             TranslateMessage (&msg);
  283.             DispatchMessage (&msg);
  284.             }
  285.     return 0;
  286. }
  287.